define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));

                //console.log(panel.size());
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'oa/buy/table1',
                    extend: {
                        index_url: 'oa/buy/table1',
                        add_url: 'oa/buy/add',
                        //edit_url: 'oa/log/edit',
                        //del_url: '',
                        multi_url: '',
                        table: 'buy',
                    },
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    commonSearch: false,
                    searchFormVisible: false,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                    return ++index;
                                }, operate: false},
                            //{field: 'admin.avatar', title: __('图片'), events: Table.api.events.image, formatter: Table.api.formatter.images, operate: false},
                            {field: 'names', title: __('名称'), operate: false},
                            {field: 'status', title: __('状态'), searchList: {"0":__('待审核'),"1":__('审核通过'),"2":__('审核驳回')}, formatter: Table.api.formatter.status,operate: false},
                            {field: 'admin_name', title: __('申请人'), operate: false},
                            {field: 'create_time', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,operate: false},
                            //{field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                            {field: 'operate', title: __('Operate'), table: table1,
                                events: Table.api.events.operate,
                                buttons: [{
                                    name: '详情',
                                    text: __('详情'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'oa/buy/edit?id={id}&type=1'
                                }],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'oa/buy/table2?is_comment='+Config.is_comment,
                    extend: {
                        index_url: 'oa/buy/table2',
                        add_url: 'oa/buy/add',
                        //edit_url: 'oa/log/edit',
                        //del_url: '',
                        multi_url: '',
                        table: 'buy',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    commonSearch: false,
                    searchFormVisible: false,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    //searchFormTemplate: 'customformtpl',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                    return ++index;
                                }, operate: false},
                            //{field: 'avatar', title: __('图片'), events: Table.api.events.image, formatter: Table.api.formatter.images, operate: false},
                            {field: 'name', title: __('名称'), operate: false},
                            {field: 'buy.status', title: __('状态'), searchList: {"0":__('待审核'),"1":__('审核通过'),"2":__('审核驳回')}, formatter: Table.api.formatter.status,operate: false},
                            {field: 'buy.admin_name', title: __('申请人'), operate: false},
                            {field: 'buy.create_time', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,operate: false},
                            //{field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                            {field: 'operate', title: __('Operate'), table: table2,
                                events: Table.api.events.operate,
                                buttons: [{
                                    name: '详情',
                                    text: __('详情'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'oa/buy/edit?id={buy.id}&type=2'
                                }],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
            third: function () {
                // 表格3
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'oa/buy/table3',
                    extend: {
                        index_url: 'oa/buy/table3',
                        add_url: 'oa/buy/add',
                        //edit_url: 'oa/log/edit',
                        //del_url: '',
                        multi_url: '',
                        table: 'buy',
                    },
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    commonSearch: false,
                    searchFormVisible: false,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    //searchFormTemplate: 'customformtpl',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                    return ++index;
                                }, operate: false},
                            //{field: 'avatar', title: __('图片'), events: Table.api.events.image, formatter: Table.api.formatter.images, operate: false},
                            {field: 'name', title: __('名称'), operate: false},
                            {field: 'buy.status', title: __('状态'), searchList: {"0":__('待审核'),"1":__('审核通过'),"2":__('审核驳回')}, formatter: Table.api.formatter.status,operate: false},
                            {field: 'buy.admin_name', title: __('申请人'), operate: false},
                            {field: 'buy.create_time', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,operate: false},
                            //{field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                            {field: 'operate', title: __('Operate'), table: table3,
                                events: Table.api.events.operate,
                                buttons: [{
                                    name: '详情',
                                    text: __('详情'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'oa/buy/edit?id={buy.id}&type=3'
                                }],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格3绑定事件
                Table.api.bindevent(table3);
            },
        },
        add: function () {
            $('.btn-append').click(function(e, obj){

                //绑定日期时间元素事件
                require(['bootstrap-datetimepicker'], function () {
                    var options = {
                        format: 'YYYY-MM-DD HH:mm:ss',
                        icons: {
                            time: 'fa fa-clock-o',
                            date: 'fa fa-calendar',
                            up: 'fa fa-chevron-up',
                            down: 'fa fa-chevron-down',
                            previous: 'fa fa-chevron-left',
                            next: 'fa fa-chevron-right',
                            today: 'fa fa-history',
                            clear: 'fa fa-trash',
                            close: 'fa fa-remove'
                        },
                        showTodayButton: true,
                        showClose: true,
                    };
                    $('.datetimepicker', obj).parent().css('position', 'relative');
                    $('.datetimepicker', obj).datetimepicker(options).on('dp.change', function (e) {
                        $(this, document).trigger("changed");
                    });
                });
            });


            Controller.api.bindevent();
        },
        edit: function () {
            table.bootstrapTable('refresh');
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});